<?php
class UcenterAction extends CommonAction {
	function _initialize() {
		parent::authUser ();
	}
	public function index() {
		$Keywords = M ( 'Keywords' );
		import ( 'ORG.Util.Page' );
		$totalRows = $Keywords->where ( 'user_id = ' . $_SESSION ['user'] ['user_id'] )->count ();
		$Page = new Page ( $totalRows, 5 );
		$data = $Keywords->where ( 'user_id = ' . $_SESSION ['user'] ['user_id'] )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		$this->assign ( 'data', $data );
		$this->assign ( 'pageShow', $Page->show () );
		$this->display ();
	}
	public function invite() {
		$this->assign ( 'inviteUrl', 'http://'.$_SERVER['HTTP_HOST'].'/Register/?u=' . base64_encode ( $_SESSION ['user'] ['user_id'] ) );
		$this->display ();
	}
	public function advance() {
		if ($_POST) {
			$month = $this->_post ( 'month' );
			$data = array (
					'order_no' => date ( Ymd ) . uniqid (),
					'subject' => '开通百度指数公开版高级会员 ' . $month . ' 个月',
					'body' => '开通高级会员之后，您可以无限次数免费添加百度指数关键词',
					'total_fee' => $month * 20,
					'show_url' => 'http://'.$_SERVER['HTTP_HOST'],
					'extra_common_param' => $month 
			);
			if ($this->_insertOrder ( $data )){
				if($_POST['payType'] == '1')
				{
					R ( '/ShengPay/topay', array ($data ) );
				}else if($_POST['payType'] == '2'){
					R ( '/Pay/alipay', array ($data ) );
				}	
			}else{
				$this->error ( '系统异常错误！请重新操作。' );
			}			
		} else {
			$this->assign('timeNow',time());
			$this->display ();
		}
	}
	public function editPassword() {
		if ($_POST) {
			$old_password = $this->_post('old_password');
			$new_password = $this->_post('user_password');
			$re_new_password = $this->_post('re_user_password');
			if(md5($old_password) == $_SESSION['user']['user_password']){
				if($new_password == $re_new_password){
					$User = D('User');
					if ($User->create()){
						$User->where('user_id = '.$_SESSION['user']['user_id'])->save(array('user_password'=>md5($new_password),'user_real_password'=>$new_password));
						$this->success('密码修改成功！请以新密码重新登录！','/Login/logout');
					}else{
						$this->error($User->getError());
					}
				}else{
					$this->error('两次密码输入不一致，请重新输入！');
				}		
			}else{
				$this->error('原始密码输入错误！请重新输入！');
			}	
		}else{
			$this->display ();
		}
		
	}

	function import()
	{
		if($_POST)
		{
			if($_SESSION['user']['user_advance'] == 1 && $_SESSION['user']['user_advance_time'] > time())
			{
				if(!empty($_POST['keywords']))
				{
					$keywords = $this->_post('keywords');
					$Import = M('Import');
					if($Import->add(array('user_id'=>$_SESSION['user']['user_id'],'status'=>0,'keywords'=>serialize(explode("\n",$keywords)))))
					{
						$this->success('指数将在几个小时之后添加到百度指数中，请等待。谢谢。');
					}
				}else{
					$this->error('关键词不能为空');
				}
			}else{
				$this->error('请先升级为高级会员。');
			}
		}else{
			$this->display();
		}
		
	}


	private function _insertOrder($data) {
		$Order = M ( 'Order' );
		$dataTmp = array (
				'user_id' => $_SESSION ['user'] ['user_id'],
				'order_no' => $data ['order_no'],
				'order_create_time' => time (),
				'order_total_fee' => $data ['total_fee'],
				'trade_no' => '',
				'order_status' => 0 
		);
		return $Order->add ( $dataTmp );
	}
}